﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using UnityScriptEngine;

namespace MBBStoreClient
{
    public partial class frmPurchaseOrderLogViewer : Form
    {
        UnityProcessor proc = new UnityProcessor();
        UnityScript[] dat = new UnityScript[0];
        Threader t = new Threader();

        public frmPurchaseOrderLogViewer()
        {
            InitializeComponent();
            InitializeFrameSliderForm();
        }
        private void InitializeForm()
        {
            proc.ProcessCompleted += new ProcessHandler(proc_ProcessCompleted);
            proc.ProcessError += new ProcessHandler(proc_ProcessError);
            t.MethodCompleted += new EventHandler(t_MethodCompleted);
            txtFilter.KeyPress += new KeyPressEventHandler(txtFilter_KeyPress);
            t.MethodCall(LoadData);
        }

        void txtFilter_KeyPress(object sender, KeyPressEventArgs e)
        {
            if(e.KeyChar == (char)13) t.MethodCall(LoadData);
        }
        void t_MethodCompleted(object sender, EventArgs e)
        {
            t.SafeCall(this, delegate
            {

            });
        }
        void proc_ProcessError(string id, string desc)
        {
            t.SafeCall(this, delegate
            {
                MessageBox.Show(desc);
            });
        }
        void proc_ProcessCompleted(string id, string desc)
        {
            t.SafeCall(this, delegate
            {
                switch (id)
                {
                    case "LOADDATA":
                        proc.DBPUX_Data_Fetch(out dat);
                        grdItems.DataSource = dat[0].ToDataTable();
                        grdItems.Columns[0].Width = (grdItems.Width * 15) / 100;
                        grdItems.Columns[1].Width = (grdItems.Width * 60) / 100;
                        grdItems.Columns[2].Width = (grdItems.Width * 20) / 100;
                        break;
                }
            });
        }

        //Operations
        void LoadData()
        {
            while (proc.IsRunning)
            {
                System.Threading.Thread.Sleep(1);
            }
            UnityScript u = new UnityScript("LOADDATA", 5);
            u.Script_Add(Script.DBPUX_BEGIN);
            if(chkAll.Checked)
                u.Script_Add(Script.DBPUX_QUERY, string.Format("SELECT fID, fUser AS 'User name', fMsg AS 'Message', fDate AS 'Date', fStamp FROM tblpurchaseorderlog WHERE " +
                    "fUser LIKE '%{0}%' OR fMsg LIKE '%{0}%' OR fDate LIKE '%{0}%' ORDER BY fStamp DESC", txtFilter.Text));
            else
                u.Script_Add(Script.DBPUX_QUERY, string.Format("SELECT fID, fUser AS 'User name', fMsg AS 'Message', fDate AS 'Date', fStamp FROM tblpurchaseorderlog WHERE " +
                    "fUser LIKE '%{0}%' OR fMsg LIKE '%{0}%' OR fDate LIKE '%{0}%' ORDER BY fStamp DESC LIMIT 0, 100", txtFilter.Text));

            u.Script_Add(Script.DBPUX_COMMIT);
            proc.Run(u);
        }
    }
}
